package com.huluobo.a2111douyindemo.login;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.huluobo.mvvm_base.mvvm.base.viewmodel.BaseViewModel;
import com.huluobo.mvvm_base.mvvm.bean.UserBeanResult;
import com.huluobo.mvvm_base.mvvm.net.Const;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/2/22.
 */
public class LoginViewModel extends BaseViewModel<LoginRepo> {
    // 成功的数据
    MutableLiveData<UserBeanResult> loginSuccess = new MutableLiveData<>();
    //失败的数据
    MutableLiveData<String> loginFailed = new MutableLiveData<>();

    public LoginViewModel(@NonNull Application application) {
        super(application);
    }

    public void login(String username, String password) {
        //封装参数,把String转换为body
        Map<String, String> map = new HashMap<>();
        map.put(Const.PARAM_USERNAME, username);//键值对赋值
        map.put(Const.PARAM_PASSWORD, password);
        String json = new Gson().toJson(map);//把map转换为一个json
        RequestBody body = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), json);//将json转换为一个body
        repo.login(body, loginSuccess, loginFailed);
    }

    @Override
    protected LoginRepo createRepo() {
        return new LoginRepo();
    }
}
